﻿using FlyingEye.DeviceMaintenance;
using FlyingEye.DeviceMaintenance.Repositories;
using FlyingEye.Exceptions;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace FlyingEye.DeviceMaintenanceServices
{
    /// <summary>
    /// 部件校验项服务
    /// </summary>
    public class DevicePartCheckItemService : ApplicationService
    {
        private readonly IDevicePartCheckItemRepository _devicePartCheckItemRepository;

        private readonly IDevicePartRepository _devicePartRepository;

        public DevicePartCheckItemService(
            IDevicePartCheckItemRepository devicePartCheckItemRepository,
            IDevicePartRepository devicePartRepository)
        {
            _devicePartCheckItemRepository = devicePartCheckItemRepository;
            _devicePartRepository = devicePartRepository;
        }

        public async Task<DevicePartCheckItemResponse> GetAsync(Guid id)
        {
            var model = await _devicePartCheckItemRepository.GetAsync(id);

            return ObjectMapper.Map<DevicePartCheckItemModel, DevicePartCheckItemResponse>(model);
        }

        /// <summary>
        /// 获取部件的校验项
        /// </summary>
        public async Task<List<DevicePartCheckItemResponse>> GetByDevicePartAsync(Guid devicePartId)
        {
            var list = await _devicePartCheckItemRepository.GetListAsync(item => item.DevicePartId == devicePartId);

            return ObjectMapper.Map<List<DevicePartCheckItemModel>, List<DevicePartCheckItemResponse>>(list);
        }

        /// <summary>
        /// 添加一个部件
        /// </summary>
        public async Task<DevicePartCheckItemResponse> InsertAsync(CreateDevicePartCheckItemRequest checkItem)
        {
            if (!await _devicePartRepository.AnyAsync(item => item.Id == checkItem.DevicePartId))
            {
                throw new ResourceNotFoundException($"部件[{checkItem.DevicePartId}]不存在。");
            }

            if (await _devicePartCheckItemRepository.AnyAsync(
                item => item.DevicePartId == checkItem.DevicePartId
                && item.Description == checkItem.Description))
            {
                throw new BusinessOperationException($"部件[{checkItem.DevicePartId}]校验内容[${checkItem.Description}]已存在。");
            }

            var model = ObjectMapper.Map<CreateDevicePartCheckItemRequest, DevicePartCheckItemModel>(checkItem);

            model = await _devicePartCheckItemRepository.InsertAsync(model);

            return ObjectMapper.Map<DevicePartCheckItemModel, DevicePartCheckItemResponse>(model);
        }

        /// <summary>
        /// 更新校验项
        /// </summary>
        public async Task<DevicePartCheckItemResponse> UpdateAsync(UpdateDevicePartCheckItemRequest checkItem)
        {
            if (!await _devicePartRepository.AnyAsync(item => item.Id == checkItem.DevicePartId))
            {
                throw new ResourceNotFoundException($"部件[{checkItem.DevicePartId}]不存在。");
            }

            if (await _devicePartCheckItemRepository.AnyAsync(
                item => item.DevicePartId == checkItem.DevicePartId
                && item.Description == checkItem.Description))
            {
                throw new BusinessOperationException($"部件[{checkItem.DevicePartId}]校验内容[${checkItem.Description}]已存在。");
            }

            var model = await _devicePartCheckItemRepository.FindAsync(checkItem.Id);

            if (model == null)
            {
                throw new ResourceNotFoundException($"校验项[{checkItem.Id}]不存在。");
            }

            ObjectMapper.Map(checkItem, model);

            model = await _devicePartCheckItemRepository.UpdateAsync(model);

            return ObjectMapper.Map<DevicePartCheckItemModel, DevicePartCheckItemResponse>(model);
        }

        /// <summary>
        /// 删除一个校验项
        /// </summary>
        public async Task DeleteAsync(Guid id)
        {
            if (!await _devicePartCheckItemRepository.AnyAsync(item => item.Id == id))
            {
                throw new ResourceNotFoundException($"校验项[{id}]不存在。");
            }

            await _devicePartCheckItemRepository.DeleteAsync(id);
        }
    }
}
